﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace Dot5Demo.Controls
{

    public abstract class CoreRender : ICoreRender
    {
        public Rectangle Bounds { get; set; }
        public Size Size { get; set; }
        public Point Location { get; set; }
        public bool Visible { get; set; }
        public bool Enable { get; set; }
        public string Name { get; set; }
        public ICoreRender Parent { get; set; }
        public List<ICoreRender> Renders { get; set; }

        public CoreRender()
        {
            Renders = new List<ICoreRender>();
        }

        public abstract void Paint(Graphics g);

        public virtual void Render(Graphics g)
        {
            if (!Visible)
                return;
            if (Bounds.IsEmpty)
                return;
            if (Renders == null || Renders.Count == 0)
                return;
            Paint(g);
            Renders.ForEach(r =>
            {
                try
                {
                    r.Render(g);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine("Render Exception \n" + ex.Message);
                }
            });
        }

        public virtual void AddRender(ICoreRender render)
        {
            if (render == null)
                return;
            if (Renders.Contains(render))
            {
                Renders.Remove(render);
            }
            render.Parent = this;
            Renders.Add(render);
        }

        public virtual void RemoveRender(ICoreRender render)
        {
            if (render == null)
                return;
            if (Renders.Contains(render))
            {
                Renders.Remove(render);
                render.Parent = null;
            }
        }
    }
}